package com.xemotion.weceem.services

import org.codehaus.groovy.grails.web.context.ServletContextHolder

import org.weceem.files.WcmContentFile
import org.weceem.files.WcmContentDirectory
import com.xemotion.weceem.files.WcmContentFileBin


/**
 * ContentFileBinService
 * @author Yasumasa Note
 *
 * Utility service for server files and cluster support.
 */
class ContentFileBinService {

    static transactional = true

    /**
     * Initialize WcmContentFileBin from WcmContentFile and server files.
     * @author Yasumasa Note
     *
     * execute only application first running.
     */
    public void initialize() {
        def contets_files = WcmContentFile.findAll()
        for (contets_file in contets_files) {
            if (contets_file && !(contets_file instanceof WcmContentDirectory)) {
                if (WcmContentFileBin.countByWcmContentId(contets_file.id) == 0) {
                    def mt = contets_file.fileMimeType ?: WcmContentFile.getDefaultMimeType(contets_file.aliasURI)
                    def resource_path = ServletContextHolder.servletContext.getRealPath(contets_file.toResourcePath())
                    def file = new File( resource_path )
                    if (file) {
                        def file_bin = new WcmContentFileBin (
                            wcmContentId: contets_file.id,
                            data: file.bytes,
                            resourcePath: contets_file.toResourcePath(),
                            relativePath: contets_file.toRelativePath(),
                            fileName: contets_file.aliasURI,
                            fileMimeType: mt,
                            fileSize: contets_file.fileSize
                        )
                        if (file_bin.save(flash:true)) {
                            log.debug "WcmContentFileBin of '${file_bin.fileName}' is saved."
                        } else {
                            log.error "WcmContentFileBin of '${file_bin.fileName}' is not saved."
                        }
                    } else {
                        log.error "WcmContentFileBin '${resource_path}' is not saved."
                    }
                }
            }
        }
    }
	
    /**
     * Restore all server files from DB if not exists.
     * @author Yasumasa Note
     */
    public void restoreAll() {
        def id_array = WcmContentFileBin.executeQuery( "select fb.id from WcmContentFileBin fb" )
        for (id in id_array) {
            def file_bin = WcmContentFileBin.get(id)
            def resource_path = ServletContextHolder.servletContext.getRealPath(file_bin.resourcePath)
			
            def file = new File( resource_path )
            if (!file.parentFile.exists()) {
                assert file.parentFile.mkdirs()
                log.debug "mkdir ${file.parent}"
            }
            if (!file.exists()) {
                file << file_bin.data
                log.debug "WcmContentFileBin of '${file_bin.fileName}' is restored."
            }
        }
    }
	
}
